/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MongoDAL;

import DALFactory.ILogin;
import Entity.EUser;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

/**
 *
 * @author kocio
 */
public class MongoLogin implements ILogin {

 private MongoStatement st;
 private MongoDataMapper sm;
 private boolean online=false;
 private EUser u=null;
 /**
  * 
  * @return 
  */
    public boolean isLogin()
    {
        return online;
    }
    /**
     * 
     * @return 
     */
    public EUser getUser()
    {
        return u;
    }
    /**
     * 
     * @param UserName
     * @param Passwd 
     */
    public  MongoLogin (String UserName,String Passwd)
    {
          st=new MongoStatement();
          sm=new MongoDataMapper();
          BasicDBObject query = new BasicDBObject();
          query.put("USERNAME", UserName);
          query.put("PASSWD", Passwd);
          DBCollection colls=st.QueryTable("User");
          DBCursor cur= colls.find(query);
          if(cur.hasNext())
          {
              online=true;
              System.out.println("LoginSuccess");
              DBObject myDoc = cur.next();
              u=sm.getUser(myDoc);
              st=null;
              sm=null;
          }
          else{
              System.out.println("LoginFailed");
              online=false;
              st=null;
              sm=null;
          }
    }
}
